<?php
/*************************************************

视频数据接口 - the
Author: Reload <17-10-31>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class VideoController extends Controller {
    public function appraisal() {
            $type = I('post.type');
            $page = I('post.page',1,int);
            $pageSize = I('post.pageSize',10,int);
            $platform_id[] = $v['id'];
                
              
                    //$map['platform_id'] = array('in',$platform_id);
                    $map['cate_id'] =3;

                    if($type==1) {
                        //国资系
                        $map['background'] = 1;
                    } elseif($type==2) {
                        //风投系
                        $map['background'] = 3;
                    } elseif($type==3) {
                        //银行系 
                        $map['background'] = 4;
                    } elseif($type==4) {
                        //上市系
                        $map['background'] = 2;
                    } elseif($type==5) {
                        //上市系
                        $map['background'] = 5;
                    }


                    //$map['house_id'] = array('neq',11);
                    $video_app = M('VideoAppraisal')->where($map)->limit(($page-1)*$pageSize,$pageSize)->select();
                    //echo  M('VideoAppraisal')->_sql();die;
                  
                    if($video_app){
                        foreach($video_app as $k => $v) {
                            $platform[$k]['add_time'] = date('Y-m-d',$v['add_time']);
                            $house_id= $v['house_id'];
                            if($house_id){
                                $map = array();
                                $map['jg_video.id'] = $house_id;
                                $field = 'jg_video.id,jg_video.video_id,jg_video.news_id,jg_video.title,jg_video.category,jg_video.pic,jg_video.hd_url,jg_video.program,jg_video.count_comments,jg_video.add_time,jg_video.label_id,jg_video_house.push_play,jg_video_house.play_amount';
                                $video_app[$k]['video_info'] = D('Video')->jion_play_one($map,$field);
                                $pic =  $video_app[$k]['video_info']['pic'];
                                 $video_app[$k]['video_info']['pic'] = preg_replace ('/http:\/\//', 'https://', $pic,1);

                            }else{
                                $map = array();
                                $map['id'] = $v['news_id'];
                                $video_app[$k]['news_info'] = D('NewsHouse')->get_one($map);
                                $pic = $video_app[$k]['news_info']['pic'];
                                $video_app[$k]['news_info']['pic'] = preg_replace ('/http:\/\//', 'https://', $pic,1);
                            }
                            if($house_id==574){
                                $map = array();
                                $map['id'] = $v['news_id'];
                                $video_app[$k]['news_info'] = D('NewsHouse')->get_one($map,$field="pic");
                                $video_app[$k]['house_id'] =0;               // 
                            }
                           

                        }
                    }
                
                $arr = array_values($video_app);
              //  print_r($video_app);
                // $a = json_encode($video_app);
                // echo $a;die;
                if($video_app){
                    $res['errorCode'] = 1;
                    $res['msg'] ='成功';
                    $res['data'] = $arr;
                }else{
                    $res['errorCode'] = 0;
                    $res['msg'] ='暂无数据';
                }
            //    print_r($res);
             $this->ajaxReturn($res);
          
    }


    //视频详情页
   //视频详情页
    public function appraisal_info() {
        $cate_id = I('get.cate_id');
        $p_id = I('get.p_id');
        if($p_id){
            $map['id'] = $p_id;
            $platform = D('Platform')->get_one($map);
            $map = array();
            $map['platform_id'] = $p_id;
            $map['cate_id'] = $cate_id;
            $video_app = M('VideoAppraisal')->where($map)->select();
            foreach ($video_app as $k => $v) {
                if( $v['house_id']){
                    $map = array();
                    $map['jg_video.id'] = $v['house_id'];
                    $field = 'jg_video.id,jg_video.video_id,jg_video.news_id,jg_video.title,jg_video.category,jg_video.pic,jg_video.hd_url,jg_video.program,jg_video.count_comments,jg_video.add_time,jg_video.label_id,jg_video_house.push_play,jg_video_house.play_amount';
                    //$video_info =  
                    $video[]= D('Video')->jion_play_one($map,$field);
                }
                if( $v['news_id']){
                    $map       = array();
                    $map['id'] = $v['news_id'];
                    $news= D('NewsHouse')->get_one($map);
                }
                
            }
          //  print_r($video_app);
        }else{
            $id = I('get.a_id');
            // echo $id;die;
            $map = array();
            $map['id'] = $id;
            $video_app = M('VideoAppraisal')->where($map)->find();
            $platform_name = $video_app['platform_name'];
            // print_r($video_app);
            // echo $video_app;die;
            $map = array();
            $map['cate_id'] = $cate_id;
            $map['platform_name'] = $platform_name;
            $video_app = M('VideoAppraisal')->where($map)->find();
            $map = array();
            $map['id'] = $video_app['house_id'];
            $video = D('VideoHouse')->get_one($map);
            $map = array();
            $map['id'] = $video_app['news_id'];
            $news = D('NewsHouse')->get_one($map);
        }
        // if(!$video_app){
        //     $video_app = array();
        // }
        if(!$video){
            $video =array();
        }
        if(!$news){
            $news =array('data'=>0);
        }

        
        $arr = array('video'=>$video,'news'=>$news);
        if($news or $video_app){
                $res['errorCode'] = 1;
                $res['msg'] = "获取成功";
                $res['data'] = $arr;
        }else{
                $res['errorCode'] = 0;
                $res['msg'] = "暂无数据";
        }
        $this->ajaxReturn($res);

    }

    public function appraisal_apply(){
        $val = I('post.val',0,int);
        if($val){
            $data = array("errorCode"=>1);
        }else{

        }

    }


    //栏目页
    public function program(){

        $id = I('post.id');
        $page = I('post.currentPage');
        $pagenum = I('post.pageSize');
        $page =($page-1)*$pagenum;
        $map = array();
        $program = $id;
        $order = 'jg_video.sort desc';
        $map['jg_video.program'] = $program;
        $map['jg_video.status'] = 0;
    //    echo $page.'*********'.$pagenum;die;
        $field = 'jg_video.id,jg_video.video_id,jg_video.news_id,jg_video.title,jg_video.category,jg_video.pic,jg_video.hd_url,jg_video.program,jg_video.count_comments,jg_video.add_time,jg_video.label_id,jg_video_house.push_play,jg_video_house.host,jg_video_house.play_amount';
        $list = $this->program_get_video($map,$order,$field,$page,$pagenum);
        foreach($list as $k=>$v){
            $map = array();
            $map['id'] = $v['host'];
            $host =  M('Host')->where($map)->field('name')->find();
            $list[$k]['host_name'] =$host['name'];

            $map = array();
            $map['id'] = $news_id;
            $news = M('NewsHouse')->where($map)->field('content_short')->find();
            $list[$k]['content_short'] =$news['content_short']?$news['content_short']:'';
            $list[$k]['title'] =msubstr($v['title'],0,23);
            $list[$k]['play_count'] =$v['push_play']+$v['play_amount'];
            $list[$k]['add_date'] =date('y-m-d',$v['add_time']);

        }
        if($list) {
            $res = array('errorCode'=>1,'data'=>$list);
        } else {
            $res = array('errorCode'=>0,'msg'=>"暂无数据");
        }
        $this->ajaxReturn($res);
    }


    protected function program_get_video($map,$order,$field,$start,$end){
        $list = D('video')->jion_play($map,$order,$field,$start,$end);

        foreach ($list as $k => $v) {
            $list[$k]['play'] = $v['play_amount']+$v['push_play'];
        }
        return $list;
    }


    //当前视频详情
    public function video_play() {
        $id = I('get.id');
        $map['id'] = $id;
        $video_info = D('Video')->get_one($map);
        if($video_info) {
            $video_id = $video_info['video_id'];
            $news_id = $video_info['news_id'];
            $map = array();
            $map['id'] =$video_id;
            $where['id'] =$news_id;

            $video_info['video_info'] = D('VideoHouse')->get_one($map);//视频详情
            $video_info['news_info'] = D('NewsHouse')->get_one($where); //新闻详情
            //节目名 主持人
            if($video_info['program']){
                $video_info['program_name'] =get_program_name($video_info['program']);
            }
            $host_id = explode(',', $video_info['host']);
            foreach ($host_id as $k => $v) {
                $host_arr[$k]['host_name'] =get_host_name($v);
                $host_arr[$k]['host_pic']=get_host_pic($v);
            }
            $video_info['host_arr'] = $host_arr;

            //标签
            $video_info['cate_name'] =get_cate_name($video_info['category']);
            $lable = explode(',',$video_info['label_id']);
            foreach($lable as $k=>$v){
                $video_info['lable_name'][]=get_lable_name($v);
            }
            $info  = $video_info;
            if($info) {
                $res = array('errorCode'=>1,'data'=>$info);
            } else {
                $res = array('errorCode'=>0,'msg'=>"暂无数据");
            }
        } else {
            $res = array('errorCode'=>0,'msg'=>"暂无数据");
        }
            $this->ajaxReturn($res);
    }


         //播放列表
    public function video_info() {
        $type = I('get.type');
        $label_id = I('get.label_id');
        if($type==1){
            //按分类查询列表
            $cate_id = I('get.category');
            $cate_name = get_cate_name($cate_id);
            $play_list = $this-> get_play_list($cate_id,$program=0,$label_id=0);
        }else if($type==2){
            //按节目查询列表
            $program = I('get.program');
            $program_name = get_program_name($program);
            $play_list = $this-> get_play_list($cate_id=0,$program,$label_id=0);
        }else if($type==3){
            //按分类下标签查询列表
            $cate_id = I('get.category');;
            $cate_name = get_cate_name($cate_id);
            $play_list = $this-> get_play_list($cate_id,$program=0,$label_id);
        }
        foreach($play_list as $k=>$v){
            $play_list[$k]['play_count'] = $v['play_amount'] + $v['push_play'];
            $p_id = $v['program'];
            $map['id'] = $p_id;//节目ID
            $program = M('Program')->where($map)->field('id,pname')->find();
            $play_list[$k]['program'] = $program['pname'];
        }
        /*$id = I('get.id');
         foreach($play_list as $k=>$v){
            $play_list[$k]['program_name'] =get_program_name($v['program']);
             if($v['id'] == $id){
                 $new_tab = $v;
                 unset($play_list[$k]);
             }
        }*/
        //echo json_encode($play_list);
    //   print_r($play_list);die;
        if($play_list) {
            $res = array('errorCode'=>1,'data'=>$play_list);
        } else {
            $res = array('errorCode'=>0,'msg'=>"暂无数据");
        }
        $this->ajaxReturn($res);
    }


    /**
     * [get_play_list 获取播放列表]
     * @param  integer $cate_id  [分类ID]
     * @param  integer $program  [节目id]
     * @param  integer $label_id [标签ID]
     * @return [type]            [array]
     */
    protected function get_play_list($cate_id=0,$program=0,$label_id=0){
        //$cate_id  =>列表为改分类下所有视频
        //$program  =>列表为改节目下所有视频
        //$label_id =>列表为该分类下所有含有该标签属性的视频
        $field = 'jg_video.id,jg_video.video_id,jg_video.news_id,jg_video.title,jg_video.category,jg_video.pic,jg_video.hd_url,jg_video.program,jg_video.count_comments,jg_video.add_time,jg_video.label_id,jg_video_house.push_play,jg_video_house.play_amount';
        if($cate_id && !$label_id){
            $category = $cate_id;
            $order = 'jg_video.sort desc';
            $map['jg_video.category'] = $category;
            $list = $this->cate_get_video($map,$order,$field,0,10);
            return $list;
        }

        if($cate_id && $label_id){
            $category = $cate_id;
            $order = 'jg_video.sort desc';
            $map['jg_video.category'] = $category;
            $list = $this->cate_get_video($map,$order,$field,0,10);

             foreach($list as $k=>$v){
                $label_arr = explode(',',$v['label_id']);

                 if(in_array($label_id, $label_arr)){
                     $arr[] = $v;
                 }
            }
             return $arr;
        }

        if($program){
            $program = $program;
            $order = 'jg_video.sort desc';
            $map['jg_video.program'] = $program;
            $list = $this->cate_get_video($map,$order,$field,0,10);
            return $list;
        }

    }


    /**
     * [cate_get_video 分类获取数据]
     * @param  [type] $map   [条件]
     * @param  [type] $order [排序]
     * @param  [type] $field [字段]
     * @param  [type] $start [起始查询]
     * @param  [type] $end   [查询条数]
     * @return [type]        [array]
     */
    protected function cate_get_video($map,$order,$field,$start,$end){

        $list = D('video')->jion_play($map,$order,$field,$start,$end);
        //return D('video')->_sql();
        foreach ($list as $k => $v) {
            $list[$k]['play'] = $v['play_amount']+$v['push_play'];
             $news_id = $v['news_id'];
            if($news_id){
                $map=array();
                $map['id'] = $news_id;
                $news_info = D('NewsHouse')->get_one($map);
                $list[$k]['news_content_short'] = $news_info['content_short'];
            }
        }
        return $list;
    }

    //视频评论列表
    public function video_comment() {
        $page = I('post.currentPage',0,int);
        $pagenum = I('post.pageSize',10,int);
     //   print_r($page);die;
        $page =($page-1)*$pagenum;
        $id_target = I('post.id_target');
        $user_id = I('post.userid');
        $comments = R('Comment/get_comments',array($userid,$id_target,$page,$pagenum,$type,$id));

    //    print_r($comments);die;
        if($user_id) {
            foreach ($comments as $k => $v) {
                $id_target = $v['id'];
                $map['id_target'] = $id_target;
                $map['id_user'] = $user_id;
                $map['type'] = 3;
                $praise = D('Praise')->get_one($map);
                if($praise) {
                    $comments[$k]['praise_status'] = $praise['status'];
                } else {
                    $comments[$k]['praise_status'] = 1;
                }
            }
        }
    //    print_r($comments);die;
        if($comments) {
            $res = array('errorCode'=>1,'data'=>$comments);
        } else {
            $res = array('errorCode'=>0,'msg'=>"暂无数据");
        }
        $this->ajaxReturn($res);
    }


    public function video_comment_info() {
        $page = I('post.currentPage',0,int);
        $pagenum = I('post.pageSize',10,int);
     //   print_r($page);die;
        $page =($page-1)*$pagenum;
        $id = I('post.id');
    //    echo $id;die;
        $id_target = I('post.id_target');
        $user_id = I('post.userid');
    //    echo $page.'++++++++'.$pagenum.'++++++++'.$id.'++++++++'.$id_target.'++++++++'.$user_id;
        $comments = R('Comment/get_comments',array($user_id,$id_target,$page,$pagenum,$type,$id));
    //    print_r($comments);die;
        if($user_id) {
            foreach ($comments as $k => $v) {
                $id_target = $v['id'];
                $map['id_target'] = $id_target;
                $map['id_user'] = $user_id;
                $map['type'] = 3;
                $praise = D('Praise')->get_one($map);
                if($praise) {
                    $comments[$k]['praise_status'] = $praise['status'];
                } else {
                    $comments[$k]['praise_status'] = 1;
                }
            }
        }
    //    print_r($comments);die;
        if($comments) {
            $res = array('errorCode'=>1,'data'=>$comments);
        } else {
            $res = array('errorCode'=>0,'msg'=>"暂无数据");
        }
        $this->ajaxReturn($res);
    }


}